home *** CD-ROM | disk | FTP | other *** search
/ Beginning Mac Programming / Beginning Mac Programming.bin / pc / Open Me for REALbasic 3 / REALbasic 3.2 / Goodies / REAL Software / REALbasic Plug-ins SDK / Examples / Box Control / boxControl.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2001-01-24  |  3.9 KB  |  162 lines

  1. #include "rb_plugin.h"
  2.  
  3. extern struct REALcontrol boxControl;
  4.  
  5. REALevent boxEvents[] = {
  6.     { "Action(Red As Integer)" }
  7. };
  8.  
  9. struct boxData
  10. {
  11.     int fillColor;            // color in RB color format (xxRRGGBB)
  12. };
  13.  
  14. static void boxInit(REALcontrolInstance)
  15. {
  16. }
  17.  
  18. #pragma mark boxDraw
  19. #ifdef TARGET_WIN32
  20. static void boxDraw(REALcontrolInstance instance, REALgraphics context)
  21. #else
  22. static void boxDraw(REALcontrolInstance instance)
  23. #endif
  24. {
  25.     // Find the bounds rect
  26.     Rect r;
  27.     REALGetControlBounds(instance, &r);
  28.  
  29.     // Make sure our aspect ratio is constant.
  30.     short height = r.bottom-r.top, width = r.right-r.left;
  31.     if (width < height) {
  32.         // shorten height to match width
  33.         height = width;
  34.         r.bottom = r.top + height;
  35.         if (REALinRuntime()) REALSetControlPosition( instance, kREALHeight, height );
  36.     } else if (height < width) {
  37.         // shorten width to match height
  38.         width = height;
  39.         r.right = r.left + width;
  40.         if (REALinRuntime()) REALSetControlPosition( instance, kREALWidth, width );
  41.     }
  42.     
  43.     // Get our custom data into "data"
  44.     ControlData(boxControl, instance, boxData, data);
  45.  
  46.     // Do the drawing (using native graphics toolbox)
  47.   #if TARGET_WIN32
  48.     // Windows implementation:
  49.  
  50.     HDC hDC = REALGraphicsDC(REALGetControlGraphics(instance));
  51.  
  52.     HBRUSH fillBrush = CreateSolidBrush(RGB(
  53.         (data->fillColor >> 16), (data->fillColor >> 8) & 0xFF, (data->fillColor & 0xFF)));
  54.     HBRUSH lineBrush = CreateSolidBrush(RGB(0,0,0));
  55.     HPEN oldPen, linePen = CreatePen(PS_SOLID, 1, RGB(0,0,0));
  56.  
  57.     RECT winr;
  58.     ::SetRect(&winr, r.left, r.top, r.right, r.bottom);
  59.     ::FillRect(hDC, &winr, fillBrush);
  60.  
  61.     ::FrameRect(hDC, &winr, lineBrush);
  62.  
  63.     oldPen = (HPEN) SelectObject(hDC, linePen);
  64.     ::MoveToEx(hDC, r.left, r.top, nil);
  65.     ::LineTo(hDC, r.right - 1, r.bottom - 1);
  66.  
  67.     SelectObject(hDC, oldPen);
  68.  
  69.     DeleteObject(fillBrush);
  70.     DeleteObject(lineBrush);
  71.     DeleteObject(linePen);
  72.  
  73.   #else
  74.     // Mac implementation:
  75.     RGBColor oldCol, fillCol, lineCol = {0,0,0};
  76.  
  77.     GetForeColor(&oldCol);
  78.     fillCol.red = (data->fillColor >> 16) * 257;
  79.     fillCol.green = ((data->fillColor >> 8) & 0xff) * 257;
  80.     fillCol.blue = ((data->fillColor & 0xff) * 257);
  81.     RGBForeColor(&fillCol);
  82.     PaintRect(&r);
  83.  
  84.     RGBForeColor(&lineCol);
  85.     FrameRect(&r);
  86.     
  87.     MoveTo(r.left, r.top);
  88.     LineTo(r.right - 1, r.bottom - 1);
  89.  
  90.     RGBForeColor(&oldCol);
  91.    #endif
  92. }
  93.  
  94. static void boxMakeRed(REALcontrolInstance instance)
  95. {
  96.     // get our private data
  97.     ControlData(boxControl, instance, boxData, data);
  98.  
  99.     // make the box red
  100.     data->fillColor = 0xFF0000;
  101.  
  102.     // invoke the event handler, if there is one
  103.     void (*fp)(REALcontrolInstance instance, int);
  104.     fp = (void (*)(REALcontrolInstance instance, int)) REALGetEventInstance(instance, &boxEvents[0]);
  105.     if (fp) fp(instance, data->fillColor >> 16);
  106.  
  107.     // just for testing, let's also try out some of the newer control methods
  108.     REALSetControlPosition( instance, kREALTop, REALGetControlPosition(instance, kREALTop) - 20 );
  109. }
  110.  
  111. static int boxColorComponent(REALcontrolInstance instance, int bit)
  112. {
  113.     ControlData(boxControl, instance, boxData, data);
  114.  
  115.     if (bit == 0)
  116.         return data->fillColor >> 16;
  117.     else if (bit == 1)
  118.         return (data->fillColor >> 8) & 0xff;
  119.     else
  120.         return (data->fillColor) & 0xff;
  121. }
  122.  
  123. REALproperty boxProperties[] = {
  124.     { "Appearance", "BackColor", "Color", REALpropInvalidate, REALstandardGetter, REALstandardSetter, FieldOffset(boxData, fillColor) }
  125. };
  126.  
  127. REALmethodDefinition boxMethods[] = {
  128.     { (REALproc) boxMakeRed, REALnoImplementation, "MakeRed" },
  129.     { (REALproc) boxColorComponent, REALnoImplementation, "ColorComponent(v as integer) as integer" },
  130. };
  131.  
  132. REALcontrolBehaviour boxBehaviour = {
  133.     boxInit,
  134.     nil,
  135.     boxDraw,
  136.     nil,
  137.     nil,
  138.     nil
  139. };
  140.  
  141. REALcontrol boxControl = {
  142.     kCurrentREALControlVersion,
  143.     "Box",
  144.     sizeof(boxData),
  145.     0,                    // for invisible controls like Timer, use: REALinvisibleControl
  146.     128,
  147.     129,
  148.     32, 32,
  149.     boxProperties,
  150.     sizeof(boxProperties) / sizeof(REALproperty),
  151.     boxMethods,
  152.     sizeof(boxMethods) / sizeof(REALmethodDefinition),
  153.     boxEvents,
  154.     sizeof(boxEvents) / sizeof(REALevent),
  155.     &boxBehaviour
  156. };
  157.  
  158. void PluginEntry(void)
  159. {
  160.     REALRegisterControl(&boxControl);
  161. }
  162.